package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.grantha.util.DB;
import com.grantha.util.Util;

/**
 * Servlet implementation class BookReservation
 */
@WebServlet("/BookReservation")
public class BookReservation extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log = Logger.getLogger(this.getClass());

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		log.info("Servlet Stated");
		// coding start

		String booknoreserved = request.getParameter("booknoreserved");
		String patrn = request.getParameter("patrn");

		log.info("booknoreserved=" + booknoreserved);
		log.info("patrn=" + patrn);
		if ((booknoreserved.equals("")) || (patrn.equals(""))) {
			out.print(Util.sendJSONMessage(false,
					"All required fields must be filled out"));
		}
		//
		else {
			
			
			
			try {
				//check book is available in the library
				ResultSet rs = DB.search("select bookno from bookregister where bookno = ?", booknoreserved);
				if(rs.next()){

					rs = DB.search("select patno from patronregister where patno = ?", patrn);
					
						if(rs.next()){
							
							ResultSet rs1 = DB.search("select bookno from reservationlist where bookno = ?", booknoreserved);
							if(rs1.next()){
								out.print(Util.sendJSONMessage(true,
										"Book reservation  is already done"));
							
						}
							
							else{
							DB.update("insert into reservationlist (bookno, patno) values (?, ?)",booknoreserved, patrn);
						
						
						 out.print("success");
						out.print(Util.sendJSONMessage(true,"Book reservation added successfully"));
						log.info("success");
						}
						//patron isn't availabe in the db
//						out.print(arg0)
						//////
						
						
							
						
						
						
						
				}else{
					//book isn't available in the library
					out.print(Util.sendJSONMessage(false,
							"Failure!!!!!! Book id  is not exist "));
					log.info("error");
					
				}
				
				}else{
					//book isn't available in the library
					out.print(Util.sendJSONMessage(false,
							"Failure!!!!!! Patron id  is not exist "));
					log.info("error");
					
				}

			} catch (Exception e) {
				// out.print("error");
				out.print(Util.sendJSONMessage(false, "Error when adding user"));
				log.error("Error");
			}

			// coding ends

			out.close();
			log.info("Servlet end");
		}

	}
}
